function toggleTeams(){
	var radioButtons = document.getElementsByName("playerTeam");
	
	for (var currBtn in radioButtons){
		if (radioButtons[currBtn].checked){
			if (radioButtons[currBtn].value == "newTeam"){
				$("#divExistingTeams").hide();
				$("#divNewTeam").show();
			} else if (radioButtons[currBtn].value == "existingTeam"){
				$("#divExistingTeams").show();
				$("#divNewTeam").hide();
			} else {
				$("#divExistingTeams").hide();
				$("#divNewTeam").hide();
			}
		}
	}
}

// Stages for submission :
// 1. Check syntactical validity of input fields.
// 2. if OK, check uniqueness of Player Username, then callback function will
// 3. Check Unique team name (if applicable), then will
// 4. Transmit to server
// 5. Callback function will continue from there.
function submitNewPlayer() {
	$(".inputError").hide();
	if (validateNewPlayer()) {
		checkUniqueUsername(); // This will be chained to other functions until sendToServer.
	}
}

function checkUniqueUsername(){
	$.ajax({
		url : "registration",
		type : "POST",
		data : {
			action : "checkUserUsername",
			playerUsername : $("#playerUsername").val()
		},
		success : function(data, status){
			if (data.isUnique == "false"){
				isValid = false;
				$("#usernameUniqueError").show();
			} else {
				checkUniqueTeamName();
			}
		}
	});
}

function checkUniqueTeamName(){
	if ($("#divNewTeam").is(':visible')){
		$.ajax({
			url : "registration",
			type : "POST",
			data : {
				action : "checkTeamName",
				teamName : $("#teamName").val()
			},
			success : function(data, status){
				if (data.isUnique == "false"){
					isValid = false;
					$("#teamUniqueError").show();
				} else {
					submitPlayer(true);
				}
			}
		});
	} else {
		submitPlayer(false);
	}
}

function submitPlayer(i_AlsoCreateTeam){
	var playerTeamID = i_AlsoCreateTeam ? 0 : $("#listOfTeams").val();
	var playerJoinStatus = i_AlsoCreateTeam ? "Approved" : "Pending";
	
	$.ajax({
		url : "servletPlayer",
		type : "POST",
		data : {
			action : "insert",
			username : $("#playerUsername").val(),
			password : $("#playerPassword").val(),
			name : $("#playerName").val(),
			joinStatus : playerJoinStatus,
			teamID : playerTeamID,
			birthday : $("#playerBirthday").val()
		},
		success : function(data, status){
			if (data.report == "success"){
				if (i_AlsoCreateTeam){
					submitNewTeam(data.playerID);
				} else {
					endOfSignUp("Successfully Registered!<br/>Please proceed to Log In.");
				}
			} else {
				endOfSignUp("Could not Register.<br/>Please contact site administrator.");
			}
		}
	});
}

function submitNewTeam(i_TeamLeaderID){
	$.ajax({
		url : "servletTeam",
		type : "POST",
		data : {
			action : "insert",
			teamName : $("#teamName").val(),
			teamLeaderID : i_TeamLeaderID
		},
		success : function(data, status){
			if (data.report == "success"){
				updatePlayerWithNewTeamID(data.teamID, i_TeamLeaderID);
			} else {
				endOfSignUp("Succesfully Registered,<br/>could not create new Team.");
			}
		}
	});
}

function updatePlayerWithNewTeamID(i_TeamID, i_PlayerID){
	$.ajax({
		url : "servletPlayer",
		type : "POST",
		data : {
			action : "update",
			playerID : i_PlayerID,
			username : $("#playerUsername").val(),
			password : $("#playerPassword").val(),
			name : $("#playerName").val(),
			joinStatus : "Approved",
			teamID : i_TeamID,
			birthday : $("#playerBirthday").val() 
		},
		success : function(data, status){
			if (data.report == "success"){
				endOfSignUp("Successfully Registered!<br/>Please proceed to Log In.");
			} else {
				endOfSignUp("Succesfully Registered,<br/>could not create new Team.");
			}
		}
	});
}

function validateNewPlayer() {
	var isValid = true;
	var username = $("#playerUsername").val(); 
	if ( username == "") {
		isValid = false;
		$("#usernameError").show();
	}	
	if ($("#playerName").val() == "") {
		isValid = false;
		$("#nameError").show();
	}
	if ($("#playerPassword").val() == "") {
		isValid = false;
		$("#passwordError").show();
	}
	if ($("#playerPasswordConfirm").val() != $("#playerPassword").val()) {
		isValid = false;
		$("#passwordConfirmError").show();
	}
	if ($("#divExistingTeams").is(':visible')){
		if ($("#listOfTeams").val() == null){
			isValid = false;
			$("#teamJoinError").show();
		}
	} else if ($("#divNewTeam").is(':visible')){
		if ($("#teamName").val() == ""){
			isValid = false;
			$("#teamNameError").show();
		}
	} else { //No radio was selected
		isValid = false;
		$("#radioError").show();
	}
	return isValid;
}

function endOfSignUp(i_MessageToUser){
	$("#divSignUp").fadeOut("medium", function (){
		$("#submitResult").html(i_MessageToUser);
		$("#submitResult").fadeIn();
	});
}